<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8">
		<title>首页</title>
	</head>
	<script type="text/javascript" src="js/jquery.min.js"></script>
	<!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
	<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu"
	 crossorigin="anonymous">

	<!-- 可选的 Bootstrap 主题文件（一般不用引入） -->
	<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap-theme.min.css" integrity="sha384-6pzBo3FDv/PJ8r2KRkGHifhEocL+1X2rVCTTkUfGk7/0pbek5mMa1upzvWbrUbOZ"
	 crossorigin="anonymous">

	<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
	<script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js" integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd"
	 crossorigin="anonymous"></script>
	<body>
		<h1>欢迎你&nbsp;<a id="qdl" href="login.html">请登录</a></h1>
		<input id="add" type="button" value="新增" onclick="add()" />
		<table id="table" class="table table-bordered table-hover">
			<tr>
				<td>编号</td>
				<td>发起人</td>
				<td>权限1</td>
				<td>权限1</td>
				<td>权限2</td>
				<td>权限2</td>
				<td>权限3</td>
				<td>权限3</td>
				<td>修改</td>
				<td>删除</td>
			</tr>
		</table>
		<input id="logout" type="button" value="登出" onclick="logout()" style="display: none;" />
	</body>
	<script>
		//页面加载时调用
		$(function() {
			islogin();
		})

		//加载完成后
		$(document).ready(function() {
		});

		//权限判断
		function QUANXIAN(id) {
			var name = "";
			//权限
			var a = "n";
			var b = "n";
			var c = "n";
			var admin=false;
			$.ajax({
				url: "http://localhost:8080/user/session",
				data: null,
				async: false,
				success: function(data) {
					console.log(data.data);
					//权限
					a = data.data.user_edit_top;
					b = data.data.user_edit_middle;
					c = data.data.user_edit_below;
					admin = data.data.user_edit_admin;
					name = data.data.user_name;
					console.log(a, b, c, name);
					//是否具有删除权限
					if(admin){
						//删除
						$.ajax({
							url:"http://localhost:8080/items/delete?ID="+id,
							success:function(data){
								alert(data.msg);
								window.location.reload()
							}
						})
					}else{
						alert("对不起你没有管理员权限...")
					}
				},
				error: function(e) {
					alert("出错");
					alert(e.responseText);
				},
			})
		}
		
		//是否登录
		function islogin(){
			$.ajax({
				url: "http://localhost:8080/items/show",
				data: null,
				async: false,
				success: function(data) {
					console.log(data);
					if (data.status == 201) {
						alert(data.msg);
						window.location = "http://localhost:8080/demo/login.html"
					}
					if (data.status == 200) {
						alert(data.msg);
						// alert(data.data);//用户名
						// $("#username").text("你好!"+data.data).css('color','red');
						// selectUserAll();
						$("#qdl").hide();
						$("h1").html("<span>欢迎你&nbsp;</span>" + data.data).css('color', 'red');
						document.getElementById("logout").style.display = "block";
						//查询所有记录
						selectAll();
					}
		
				},
				error: function(e) {
					alert("出错");
					alert(e.responseText);
					window.location = "http://localhost:8080/demo/login.html"
				},
			})
		}


		//查询所有记录
		function selectAll() {
			let tBody = $("#table");
			$.ajax({
				url: "http://localhost:8080/items/selectAll",
				success: function(data) {
					console.log("hahah")
					console.log(data.data)
					//拿到用的信息
					let list = data.data;
					//迭代list集合 追加新内容
					list.forEach(item => { //item代表集合中的一个元素
						console.log(item.id);
						tBody.append(doCreateRow(item))
						// console.log(tBody)
					})
				},
				error: function(e) {

				},
			})
		}

		//添加表格
		function doCreateRow(item) {
			var a = "t" + `${item.id}`;
			return `<tr id="` + a +
				`">
						<td>${item.id}</td>
						<td>${item.create}</td>
						<td>${item.role_A_1}</td>
						<td>${item.role_A_2}</td>
						<td>${item.role_B_1}</td>
						<td>${item.role_B_2}</td>
						<td>${item.role_C_1}</td>
						<td>${item.role_C_2}</td>
						<td><Button onclick="updataItemById(${item.id})">编辑</td>
						<td><Button onclick="delectItem(${item.id})">删除</td>
					</tr>`
		}

		//编辑信息
		function updataItemById(id) {
			alert(id)
			{
				window.open('datailitem.html?id=' + id, 'content',
					'width=480,height=400,top=150,left=280, toolbar=no, menubar=no, scrollbars=auto, resizable=no, location=no, status=no'
				);
			}
		}
		
		//删除信息
		function delectItem(id){
			QUANXIAN(id);
		}
		
		//新增用户
		function add() {
			{
				window.open('datailitem.html', 'content',
					'width=480,height=400,top=150,left=280, toolbar=no, menubar=no, scrollbars=auto, resizable=no, location=no, status=no'
				);
			}
		}

		//退出
		function logout() {
			$.ajax({
				url: "http://localhost:8080/user/logout",
				data: null,
				success: function(result) {
					alert("退出成功");
					location.reload();
				},
			})
		}
	</script>
</html>
